package com.ly.jdk8.stream;

import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.Stream;

/**
 * @Classname TestStream
 * @Description TODO
 * @Date 2020/9/29 12:05
 * @Author 冷心影翼
 */
public class TestStream {
	private static List<User> u = new ArrayList<>();

	public static void main(String[] args) {
		u.add(new User(18, "A", 0));
		u.add(new User(18, "AA", 0));
		u.add(new User(20, "B", 1));
		u.add(new User(18, "C", 0));
		u.add(new User(16, "D", 1));
		u.add(new User(28, "E", 0));
		u.add(new User(26, "A", 0));
		u.add(new User(25, "F", 1));
		p1();
		p2();
		p3();
	}


	//返回数组元素的平方
	public static void p1() {
		List<Integer> collect = Stream.of(1, 2, 3, 4, 5).map(it -> it * it).collect(Collectors.toList());
		System.out.println(collect);
	}

	//统计个数
	public static void p2() {
		Long collect = u.stream().collect(Collectors.counting());
		System.out.println(collect);
	}

	//找到性别0的人，并按照age从小到大，相同年龄按照名字自然排序
	public static void p3() {
		List<User> collect = u.stream().filter(it -> it.getSex() == 0)
			.sorted(Comparator.comparingInt(User::getAge).thenComparing(User::getName))
			.collect(Collectors.toList());
		System.out.println(collect);
	}
}
